<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<title>预充值返券管理系统</title>
		<link rel="stylesheet" href="css/reset.css" />
		<link rel="stylesheet" href="js/ui/jquery-ui.min.css" />
        <link rel="stylesheet" href="css/table.css" />
        <link rel="stylesheet" href="css/platform.css"/>
        <link rel="stylesheet" href="css/promotionRules.css" />
        <!--<link rel="stylesheet" href="css/refundManager.css" />-->
        <!--<link rel="stylesheet" href="css/titleDropDown.css" />-->
	</head>
	<body>
	    <div class="printer-container" ms-controller="mainController">
            <div class="header">
                <div class="u-title">促销规则</div>
            </div>
            <div class="center">
                <div class="search-container" ms-controller="searchController">
                    <div class="u-multiform">
                        <div class="column">
                            <div class="cell">
                                <label>关键字：</label>
                                <div class="cnt-box">
                                    <div class="combox">
                                        <input class="u-ipt" ms-duplex="keyWords" placeholder="门店代码/活动编码" />
                                    </div>
                                                                        
                                </div>
                            </div> 
                            <div class="cell">
                                <label>活动时间：</label>
                                <div class="cnt-box">
                                    <input class="u-ipt ipt-timepicker" type="text" id="startTime" ms-duplex="promoStart">
                                    <span class="split">-</span>
                                    <input class="u-ipt ipt-timepicker" type="text" id="endTime" ms-duplex="promoEnd">
                                </div>
                            </div> 
                        </div>
                        
                        <div class="column btn-box">
                            <div class="cell">
                                <label></label>
                                <div class="cnt-box">
                                    <button class="u-btn small" ms-click="searchData">查询</button>
                                </div>
                            </div>
                        </div>                        
                    </div>                    
                </div> 
                <div class="result">
                    <div id="testTable" ms-controller="testTable"></div>
                    <script type="avalon" id="tablerowtemplate">                         
                        <div class="tableWrapper">
                            <table class="table">
                               <thead>
                                    <tr>
                                        <th></th>
                                        <th>促销活动ID</th>
                                        <th>活动编码</th>
                                        <th>门店代码</th>
                                        <th>活动规则类型</th>
                                        <th>券有效天数</th>
                                        <th>返券次数</th>
                                        <th>劵生效日期</th>
                                        <th>劵失效日期</th>
                                        <th>活动开始日期</th>
                                        <th>活动截止日期</th>
                                        <th>上下架状态</th>
                                        <th>活动失效类型</th>
                                        <th>券名称</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr ms-repeat-row="sources">
                                        <td>{{(index-1)*peerpagerows+$index+1}}</td>
                                        <td>{{row.promotionId}}</td>
                                        <td>{{row.ruleCode}}</td>
                                        <td>{{row.storeCode}}</td>
                                        <td>{{row.ruleType | rtFilter}}</td>
                                        <td>{{row.effectiveDays}}</td>
                                        <td>{{row.maxReturn}}</td>
                                        <td>{{row.effectiveDate}}</td>
                                        <td>{{row.invalidDate}}</td>
                                        <td>{{row.promoStartTime}}</td>
                                        <td>{{row.promoEndTime}}</td>
                                        <td>{{row.promoStatus | itFilter}}</td>
                                        <td>{{row.promoOperateType | piFilter}}</td>
                                        <td ms-attr-title={{row.couponName}}>{{row.couponName | prfilter}}</td>                                        
                                        <td class="operate">                                            
                                            <a href="javascript:;" ms-click="customFuncs.toggleStatus(this,row.promotionId,row.storeCode)">{{row.promoStatus | itFilterTwo}}</a>
                                        </td>
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="15">
                                            <div class="pager">
                                                <div class="first-page" ms-click="firstpage" ms-class={{firstpagestyle}}>首页</div>
                                                <div class="pre-page" ms-click="prepage" ms-class={{prepagestyle}}>上一页</div>
                                                <div class="current-page">第<input class="current-page-num" type="text" ms-duplex="currentpage" ms-input="jumppage()"/>页
                                                    <span class="totalpage">共 {{totalpage}} 页</span>
                                                </div>
                                                <div class="next-page" ms-click="nextpage" ms-class={{nextpagestyle}}>下一页</div>
                                                <div class="last-page" ms-click="lastpage" ms-class={{lastpagestyle}}>尾页</div>
                                                <div class="pageRowsConfig" ms-if="isshowRowsList"><span class="configTxt">每页</span><select class="rowsConfig" ms-duplex="peerpagerows" ms-change="refresh"><option ms-repeat-options="rowsConfigList">{{options}}</option></select> <span class="configTxt">行</span></div>
                                                <div class="messageinfo">{{startrow}}-{{endrow}} 共{{total}}条</div>
                                            </div>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </script>
					 <div class="u-popup" id="popup_tips">
                <div class="bg"></div>
                <div class="asTable">
                    <div class="asTableCell">
                        <div class="asCell alert">
                            <a href="javascript:;" class="close btn-close">×</a>
                            <p class="title">提示</p>
                            <div class="container">
                                <div class="error"></div>
                                <p></p>
                            </div>
                            <div class="btn-box">
                                <button class="u-btn btn-close">确定</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
                    <div class="u-popup" id="popup_updateStatus">
                        <div class="bg"></div>
                        <div class="asTable">
                            <div class="asTableCell">
                                <div class="asCell alert">
                                    <a href="javascript:;" class="close btn-close">×</a>
                                    <p class="title">提示</p>
                                    <div class="container">
                                        <div class="warning"></div>
                                        <p>确定要下架吗？</p>
                                    </div>
                                    <div class="btn-box">
                                        <button class="u-btn btn-close updateStatus">确定</button>
                                        <button class="u-btn gray btn-close">取消</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="u-popup" id="popup_bigimg">
                        <div class="bg"></div>
                        <div class="asTable">
                            <div class="asTableCell">
                                <div class="asCell dialog">
                                    <a href="javascript:;" class="close btn-close">×</a>
                                    <p class="title">图片预览</p>
                                    <div class="container preview">
                                        <img ms-attr-src="prev_img" />
                                    </div>                                           
                                    <div class="btn-box">
                                        <button class="u-btn btn-close" ms-click="showImg()">确定</button>
                                    </div>                                    
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="u-popup" id="popup_cut">
                        <div class="bg"></div>
                        <div class="asTable">
                            <div class="asTableCell">
                                <div class="asCell dialog">
                                    <a href="javascript:;" class="close btn-close">×</a>
                                    <p class="title">图片裁剪</p>
                                    <div class="container">
                                        <form id="form_uploadScaleImg" enctype="multipart/form-data" method="post">
                                            <div class="upload-img">
                                                <p class="as-btn">选择文件</p>
                                                <input type="file" name="upfile" id="cropper-input">
                                            </div>
                                            <div class="preview-container">
                                                <input type="hidden" name="x" id="preview-x"/>
                                                <input type="hidden" name="y" id="preview-y"/>
                                                <input type="hidden" name="width" id="preview-width"/>
                                                <input type="hidden" name="height" id="preview-height"/>
                                                <input type="hidden" name="type" value="2"/>
                                                <div class="image-container target" id="cropper-target">
                                                  <img src="" class="noavatar" />
                                                </div>
                                                <div class="large-wrapper">
                                                  <div class="image-container large" id="preview-large">
                                                    <div class="image-wrapper">
                                                      <img src="" class="noavatar" />
                                                    </div>
                                                  </div>                                               
                                                </div>
                                            </div>
                                        </form>
                                    </div>                                           
                                    <div class="btn-box">
                                        <button class="u-btn btn-close" ms-click="uploadCityImg()">上传</button>
                                        <button class="u-btn gray btn-close">取消</button>
                                    </div>                                    
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- 弹框-内容 -->
                    <div class="u-popup" id="popup-maintain">
                        <div class="bg"></div>
                        <div class="asTable">
                            <div class="asTableCell">
                                <div class="asCell dialog">
                                    <a href="javascript:;" class="close btn-close">×</a>
                                    <p class="title">维护活动规则</p>
                                    <div class="container">
                                        <div class="u-form">
                                            <div class="column">
                                                <label>维护规则：</label>
                                                <textarea class="u-textarea rules" type="text" placeholder="请输入" ></textarea>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="btn-box">
                                        <button class="u-btn submitRules" type="button">提交</button>
                                        <button class="u-btn orange btn-close" type="button">取消</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="u-headTip"><p></p></div>
        <script src="js/jquery.min.js"></script>
        <script src="js/avalon.shim.sn.js"></script>
        <script src="js/jquery.form.js"></script>
        <script src="js/ui/jquery-ui.min.js"></script>
        <script src="js/ui/jquery-ui-timepicker-addon.js"></script>        
        <script src="js/swift-ui-table-1.1.js"></script>
        <script src="js/DataInterface/DataInterface.js"></script>
        <script src="js/platform.js"></script>
        <!--<script src="http://10.27.20.33:8080/rap.plugin.js?projectId=15"></script>-->
        
        <script>
            function filterOS(Obj){
                storeTable.searchParam.promoStatus=$(Obj).data("key");
                storeTable.search(storeTable.searchParam);
            }
            function filterType(Obj){
                storeTable.searchParam.promoStatus=$(Obj).data("key");
                storeTable.search(storeTable.searchParam);
            }
            $(function(){
                var mainCtrl = avalon.define({
                    $id: "mainController",
                    prev_img: "",
                    uploadCityImg:function(){
                        $("#form_uploadScaleImg").ajaxSubmit({                            
                            type:"post", 
                            url:DI.uploadScaleImg,
                            success: function(result){
                                if(result.retFlag == 1 || result.retFlag == "1"){
                                    var promotionId = $("#popup_cut").data("promotionId");
                                    $.ajax({
                                        type: "post",
                                        dataType: "json",
                                        data:{promotionId:promotionId,
                                                url:result.url},
                                        url: DI.saveStoreImage,
                                        success: function(data){
                                            if(data.retFlag == 1 || data.retFlag == "1"){
                                                PLATFORM.headTip("操作成功","#15b374");
                                                storeTable.refresh();
                                            }else{
                                                PLATFORM.headTip("保存失败，失败原因为："+data.errorMessage);
                                            } 
                                        },
                                        error: function(err){
                                            console.log(err);
                                        }
                                    });
                                }else{
                                    PLATFORM.headTip("图片裁剪上传失败，失败原因为："+data.errorMessage);
                                }
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown){
                                console.log("ajax error");
                                console.log(XMLHttpRequest);
                            }
                        });
                    },
                    showImg: function(){
                        var imgurl = $("#popup_bigimg").data("imgurl");
                        
                    }
                });
                var searchCtrl = avalon.define({
                    $id: "searchController",
                    keyWords: "",
                    promoStart:"",
                    promoEnd:"",
                    searchData: function(){
                        var param = {keyWords:searchCtrl.keyWords,
                                    promoStart:searchCtrl.promoStart,
                                    promoEnd:searchCtrl.promoEnd};
                        storeTable.search(param);
                    }
                });
                var opts = {
                    targetId: "testTable",
                    templateId: "tablerowtemplate",
                    settings: {
                        url:DI.promotionList,
                        ajaxType:"json",
                        peerpagerow:10,
                        sourceInterface:{INum:"total",IData:"rows"},
                        paginationIndexName:{
                            "peerpageRows":'pageSize',
                            "currentPage":'pageNo'
                        },
                        ajaxDataHandler:function(data){
                            var temp={};
                            temp.total=data.data.totalSize;
                            temp.rows=data.data.records;
                            return temp;
                        }
                    },
                    customFuncs: {
                        toggleStatus:function(Obj,ID,storeCode){
                            var txt=$(Obj).html();
                            if(txt==="上架"){
                                $(Obj).html("下架");
                                $.ajax({
                                    type:"post",
                                    dataType:"json",
                                    data:{
                                        promotionId:ID,
										storeCode:storeCode,
                                        type:1
                                    },
                                    url:DI.updatePromotionStatus,
                                    success:function(data){
										PLATFORM.showTips("success", "操作成功");
                                        storeTable.refresh();
                                    }
                                });
                            }else if(txt==="下架"){
                                $(Obj).html("上架");
                                $(".u-btn.btn-close.updateStatus").data("promotionID",ID);
                                $(".u-btn.btn-close.updateStatus").data("promotionType",0);
                                $(".u-btn.btn-close.updateStatus").data("storeCode",storeCode);
                                PLATFORM.popupShow("#popup_updateStatus");
                        }
                            
                        },
                        showOrder:function(Obj){
                            document.onclick = function (event){     
                                var e = event || window.event;  
                                var elem = e.srcElement||e.target;
                                console.log(elem.className);
                                var comboxCls = elem.parentNode.className;
                                if(elem.className!="statusTh" && elem.className!="typeTh" && elem.className!="icon-angle-down icon-large"){
                                    $("#orderStatusList").parent().attr("hasList","hide");
                                    $("#orderStatusList").slideUp(300);
                                    $("#typeList").parent().attr("hasList","hide");
                                    $("#typeList").slideUp(300);
                                }
                            }
                            if($(Obj).attr("hasList")){
                                if($(Obj).attr("hasList")==="show"){
                                    $(Obj).attr("hasList","hide");
                                    $("#orderStatusList").slideUp(300);
                                }else{
                                    $(Obj).attr("hasList","show");
                                    $("#orderStatusList").slideDown(300);
                                    $("#typeList").parent().attr("hasList","hide");
                                    $("#typeList").slideUp(300);
                                }
                            }else{
                                var content="<ul id='orderStatusList' class='hoverList'>";
                                for(var i=0;i<maps["status"].length;i++){
                                    content+="<li onclick='filterOS(this)' data-key='"+maps["status"][i].key+"'>"+maps["status"][i].desc+"</li>";
                                }
                                content+="</ul>";
                                $(Obj).css({'position':'relative'});
                                $(Obj).append(content);
                                $(Obj).attr("hasList","show");
                                $("#orderStatusList").slideDown(300);
                                $("#typeList").parent().attr("hasList","hide");
                                $("#typeList").slideUp(300);
                            }
                            
                        },
                        showType:function(Obj){
                            if($(Obj).attr("hasList")){
                                if($(Obj).attr("hasList")==="show"){
                                    $(Obj).attr("hasList","hide");
                                    $("#typeList").slideUp(300);
                                }else{
                                    $(Obj).attr("hasList","show");
                                    $("#typeList").slideDown(300);
                                    $("#orderStatusList").parent().attr("hasList","hide");
                                    $("#orderStatusList").slideUp(300);
                                }
                            }else{
                                var content="<ul id='typeList' class='hoverList'>";
                                for(var i=0;i<maps["invalidType"].length;i++){
                                    content+="<li onclick='filterType(this)' data-key='"+maps["invalidType"][i].key+"'>"+maps["invalidType"][i].desc+"</li>";
                                }
                                content+="</ul>";
                                $(Obj).css({'position':'relative'});
                                $(Obj).append(content);
                                $(Obj).attr("hasList","show");
                                $("#typeList").slideDown(300);
                                $("#orderStatusList").parent().attr("hasList","hide");
                                $("#orderStatusList").slideUp(300);
                            }
                        },
                        maintainRules:function(Obj,ID,promoRule){
                            $(".u-btn.submitRules").data("ID",ID);
                            $(".u-textarea.rules").val(promoRule);
                            PLATFORM.popupShow("#popup-maintain");
                        }
                    }
                };
                storeTable = SwiftUI.swiftTable.getInstance(opts);
                PLATFORM.datepickerRange("#startTime", "#endTime");
                avalon.scan();
                avalon.filters.itFilter=function(str){
                    var ret = "-";
					if(str == 0){
						ret = "下架";
					}else if(str == 1){
						ret = "上架";
					}else if(str == 2){
						ret = "已结束";
					}
                    return ret;
                }
                avalon.filters.piFilter=function(str){
                    var ret = "-";
					if(str == 1){
						ret = "POS失效";
					}else if(str == 2){
						ret = "Job失效";
					}else if(str == 3){
						ret = "店长手动下架";
					}else if(str == 4){
						ret = "店长手动上架";
					}else if(str == 5){
						ret = "后台手动下架";
					}else if(str == 6){
						ret = "后台手动上架";
					}
                    return ret;
                }
                avalon.filters.rtFilter=function(str){
                    var ret = "-";
					if(str == "0"){
						ret = "满";
					}else if(str == "1"){
						ret = "每";
					}
                    return ret;
                }
                avalon.filters.prfilter=function(str){
                    if(str.length>10){
                        return str.substr(0,9)+"...";
                    }else if(str.length<10 && str.length>0){
                        return str;
                    }else{
                        return "-";
                    }
                    
                }
                avalon.filters.itFilterTwo=function(str){
                    if(str == 1){
                        console.log("run in here");
                        return "下架";
                    }else if(str == 0 || str==2){
                        return "上架";
                    }
                }
                $(".u-btn.submitRules").click(function(){
                    $.ajax({
                       type:"post",
                       dataType:"json",
                       data:{
                           promotionId:$(".u-btn.submitRules").data("ID"),
                           promoRule:$(".u-textarea.rules").val()
                       },
                       url:DI.maintainRules,
                       success:function(data){
                           if(data.retFlag==1 || data.retFlag=="1"){
                               PLATFORM.headTip("操作成功","#15b374");
                               $("#popup-maintain").hide();
                               storeTable.refresh();
                           }else{
                               PLATFORM.headTip("维护失败，失败原因为："+data.errorMessage);
                           }
                       },
                       error:function(a,b,c){
                           console.log(b+" : "+c);
                       }
                       
                    });
                });
               $(".u-btn.btn-close.updateStatus").click(function(){
                   var ID = $(".u-btn.btn-close.updateStatus").data("promotionID");
                   var type= $(".u-btn.btn-close.updateStatus").data("promotionType");
				   var storeCode = $(".u-btn.btn-close.updateStatus").data("storeCode");
                   $.ajax({
                        type:"post",
                        dataType:"json",
                        data:{
                            promotionId:ID,
                            storeCode:storeCode,
                            type:type
                        },
                        url:DI.updatePromotionStatus,
                        success:function(data){
							PLATFORM.showTips("success", "操作成功");
                            $("#popup_updateStatus").hide();
                            storeTable.refresh();
                        }
                    });
               });
            });
        </script>
	</body>
</html>
